package zero.tech.games.gate.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import zero.tech.core.data.Resp;
import zero.tech.core.data.SimpleUser;
import zero.tech.games.common.Code;
import zero.tech.games.common.RoomNodeUtil;
import zero.tech.games.common.TUserAccountUtil;
import zero.tech.games.common.data.RoomNode;
import zero.tech.games.entity.games.CfGame;
import zero.tech.games.entity.games.Club;
import zero.tech.games.entity.games.ClubGameRule;
import zero.tech.games.entity.games.ClubMember;
import zero.tech.games.entity.games.ClubMemberPermission;
import zero.tech.games.entity.games.ClubMemberProxy;
import zero.tech.games.entity.games.ClubMemberRole;
import zero.tech.games.entity.user.TUserAccount;
import zero.tech.games.gate.data.entity.RoomNodeBasic;
import zero.tech.games.gate.data.res.ResClubDetail;
import zero.tech.games.gate.data.res.ResClubList;
import zero.tech.games.gate.data.res.ResClubRooms;
import zero.tech.games.gate.hot.ClubUtil;
import zero.tech.games.gate.service.api.IClubService;

@Service
public class ClubServiceImpl implements IClubService{
	@Autowired
	private RoomNodeUtil roomNodeUtil;
	@Autowired
	private ClubUtil clubUtil;
	@Autowired
	private TUserAccountUtil userAccountUtil;
	@Override
	public Resp clubList(SimpleUser simpleUser) {
		int uid=simpleUser.getUid();
		List<Club> clubs=clubUtil.loadHotClubList(uid);
		ResClubList resClubList=new ResClubList();
		resClubList.getClubs().addAll(clubs);
		return Resp.build(Code.OK, "查询茶馆列表成功",resClubList);
	}
	@Override
	public Resp clubDetail(SimpleUser simpleUser, Club club, ClubMember clubMember) {
		int club_id=club.getClub_id();
		List<ClubGameRule> clubGameRules = clubUtil.loadHotClubGameRules(club_id);
		List<CfGame> clubGames=clubUtil.loadHotClubGames(club_id);
		ClubMemberRole clubMemberRole=clubUtil.getClubMemberRole(club_id,simpleUser.getUid());
		ClubMemberPermission clubMemberPermission=clubUtil.getClubMemberPermission(club_id, simpleUser.getUid());
		ClubMemberProxy clubMemberProxy=clubUtil.getClubMemberProxy(club_id, simpleUser.getUid());
		ResClubDetail resClubDetail=new ResClubDetail();
		resClubDetail.setClub(club);
		resClubDetail.setClubMember(clubMember);
		resClubDetail.getGames().addAll(clubGames);
		resClubDetail.getClubGameRules().addAll(clubGameRules);
		resClubDetail.setRole(clubMemberRole.getRole());
		resClubDetail.setClubMemberPermission(clubMemberPermission);
		resClubDetail.setClubMemberProxy(clubMemberProxy);
		return Resp.build(Code.OK, "茶馆详情获取成功",resClubDetail);
	}
	@Override
	@Transactional
	public Resp clubCreate(SimpleUser simpleUser, String club_name) {
		TUserAccount userAccount=userAccountUtil.getUserAccount(simpleUser.getUid());
		if(userAccount.getDiamond()<1000) {
			return Resp.build(Code.DIAMOND_NOT_ENOUGH, "砖石不足");
		}
		clubUtil.createClub(simpleUser,club_name);
		return Resp.build(Code.OK, "创建成功");
	}
	@Override
	public Resp clubRooms(SimpleUser simpleUser, Club club, int game_id, int rule_id, int from, int to) {
		return clubRoomsFromRange(simpleUser,club,game_id,rule_id,from,to);
	}
	private Resp clubRoomsFromRange(SimpleUser simpleUser, Club club, int game_id, int rule_id, int from, int to) {
		if(from>to) {
			return Resp.build(Code.PARAM_ERROR, "参数错误");
		}
		String pipei_key=null;
		if(rule_id!=-1) {
			ClubGameRule clubGameRule=clubUtil.loadHotClubGameRule(rule_id);
			if(clubGameRule==null) {
				return Resp.build(Code.CLUBRULE_NOT_EXIST, "没有该玩法");
			}
			pipei_key=clubGameRule.getPipei_key();
		}
		int club_id=club.getClub_id();
		String club_id_str=String.valueOf(club_id);
		List<RoomNode> list=new ArrayList<RoomNode>();
		List<RoomNode> result=new ArrayList<>();
		int total_rooms=0;
		Map<Integer,Map<String,Map<String,RoomNode>>>map=roomNodeUtil.getSecondCacheRoomsByClubId(club_id_str);
		if(game_id==-1) {
			//全部游戏全部玩法
			for(Map<String,Map<String,RoomNode>> mmap:map.values()) {
				for(Map<String,RoomNode> mmmap:mmap.values()) {
					list.addAll(mmmap.values());
				}
			}
			
		}else {
			Map<String,Map<String,RoomNode>>mmap=map.get(game_id);
			if(mmap!=null) {
				if(StringUtils.isBlank(pipei_key)) {
					//所有玩法
					for(Map<String,RoomNode> mmmap:mmap.values()) {
						list.addAll(mmmap.values());
					}
				}else {
					//指定玩法
					Map<String,RoomNode> mmmap=mmap.get(pipei_key);
					if(mmmap!=null) {
						list.addAll(mmmap.values());
					}
				}
			}
		}
		
		
		total_rooms=list.size();
		Collections.sort(list,new Comparator<RoomNode>() {

			@Override
			public int compare(RoomNode o1, RoomNode o2) {
				int left1=o1.getMax_player()-o1.getCur_player();
				int left2=o2.getMax_player()-o2.getCur_player();
				return left2-left1;
			}
		});
		int start=from;
		if(start<total_rooms) {
			int end=to;
			end=end>=total_rooms?total_rooms:end;
			result.addAll(list.subList(start, end));
		}
		List<RoomNodeBasic> roomNodeBasics=new ArrayList<>();
		for(RoomNode roomNode:result) {
			RoomNodeBasic roomNodeBasic = new RoomNodeBasic();
			roomNodeBasic.setName(roomNode.getName());
			roomNodeBasic.setMax_player(roomNode.getMax_player());
			roomNodeBasic.setRoom_id(roomNode.getRoom_id());
			roomNodeBasic.setClub_id(roomNode.getClub_id());
			roomNodeBasic.setGame_id(roomNode.getGame_id());
			roomNodeBasic.setService_id(roomNode.getService_id());
			roomNodeBasic.setPipei_key(roomNode.getPipei_key());
			roomNodeBasic.getSeats().putAll(roomNode.getSeats());
			roomNodeBasics.add(roomNodeBasic);
		}
		
		ResClubRooms resClubRooms = new ResClubRooms();
		resClubRooms.getRoom_nodes().addAll(roomNodeBasics);
		resClubRooms.setTotal_rooms(total_rooms);
		resClubRooms.setWait_rooms(0);
		resClubRooms.setFrom(from);
		resClubRooms.setTo(to);
		return Resp.build(Code.OK, "分页获取成功",resClubRooms);
	}
}
